let list = null
let hot = null
let newTxt = null
let newShow = null
// user/userDataList
let userDataList = null
let userGood = null
async function get(obj) {
  list = await axios({
    url: 'index/index',
  }).then(res => {
    return res.data.data
  })
  hot = await axios({
    url: 'index/recommend',
  }).then(res => {
    return res.data.data
  })
  newTxt = await axios({
    url: 'index/hot',
  }).then(res => {
    return res.data.data
  })
  newShow = await axios({
    url: 'index/show',
    params: {
      id: obj,
    },
  }).then(res => {
    console.log(res)
    return res.data.data
  })
  if (localStorage.getItem('token')) {
    userDataList = await axios({
      url: 'user/userDataList',
      headers: { token: localStorage.getItem('token') },
      params: {
        type: 1,
      },
    }).then(res => {
      console.log(res)
      return res.data.data.list
    })
    userGood = await axios({
      url: 'user/userDataList',
      headers: { token: localStorage.getItem('token') },
      params: {
        type: 2,
      },
    }).then(res => {
      console.log(res.data.data.list)
      return res.data.data.list
    })
  }

  new Vue({
    el: '.shw',
    data: {
      allCate: list.allCate,
      banner: list.banner,
      sideBanner: list.sideBanner,
      allTag: list.allTag,
      allLink: list.allLink,
      list: hot.list,
      newlist: newTxt.list,
      newShow: newShow,
      naxtId: newShow.next.length === 0 ? loc[1] : newShow.next[0].id,
      naxtTitle: newShow.next.length === 0 ? '没有了' : newShow.next[0].title,
      prevId: newShow.prev.length === 0 ? loc[1] : newShow.prev[0].id,
      prevTitle: newShow.prev.length === 0 ? '没有了' : newShow.prev[0].title,
      token: localStorage.getItem('token'),
      user: localStorage.getItem('user')
        ? JSON.parse(localStorage.getItem('user'))
        : null,
      userDataList,
      userGood,
      loc: localStorage.getItem('token')
        ? userGood.data.some(({ id }) => id == loc)
        : '',
      loc1: localStorage.getItem('token')
        ? userDataList.data.some(({ id }) => id == loc)
        : '',
    },
    filters: {
      www(val) {
        let day = new Date(val).getDate()
        day = day < 10 ? '0' + day : day
        let m = new Date(val).getMonth() + 1
        m = m < 10 ? '0' + m : m
        let y = new Date(val).getFullYear()
        y = y < 10 ? '0' + y : y
        return y + '-' + m + '-' + day
      },
    },
    conputed: {},
    methods: {
      naxt(e) {
        if (this.naxtTitle == '没有了') {
          e.preventDefault()
        }
      },
      prev(e) {
        if (this.prevTitle == '没有了') {
          e.preventDefault()
        }
      },
      collection() {
        if (this.token) {
          axios({
            url: 'user/userDataHandle',
            headers: { token: this.token },
            params: {
              type: 1,
              article_id: loc[1],
              action: this.loc1 ? 'del' : 'add',
            },
          }).then(res => {
            console.log(res)
            if (res.status != 200) return layer.msg('修改失败')
            layer.msg(this.loc1 ? '取消收藏' : '收藏成功')
            this.loc1 = !this.loc1
          })
        } else {
          layer.msg('请登录')
        }
      },
      zan(e) {
        if (this.token) {
          console.log(this.loc)
          axios({
            url: 'user/userDataHandle',
            headers: { token: this.token },
            params: {
              type: 2,
              article_id: loc[1],
              action: this.loc ? 'del' : 'add',
            },
          }).then(res => {
            if (res.status != 200) return layer.msg('修改失败')
            layer.msg(res.data.data)
            this.loc ? this.newShow.info.click-- : this.newShow.info.click++
            this.loc = !this.loc
          })
        } else {
          layer.msg('请登录')
        }
      },
      rem() {
        layer.confirm(
          '确认退出？',
          {
            btn: ['确认', '取消'], //可以无限个按钮
          },
          function (index, layero) {
            //按钮【按钮一】的回调
            localStorage.clear()
            history.go(0)
          },
          function (index) {
            //按钮【按钮二】的回调
          },
        )
      },
    },
  })
}

let loc = location.search.substring(1).split('=')
console.log(loc[1])
get(loc[1])

console.log(localStorage.getItem('token'))
